package com.sh.data.engine.domain.integration.offline.model.domain;

import lombok.Data;

import java.lang.reflect.Field;
import java.sql.Types;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/19 10:40
 */
@Data
public class FieldInfo {
    // 字段名
    private String fieldName;

    // 表名
    private String tableName;

    // 字段类型（从db中读取到的）
    private String fieldType;

    // 字段类型（对应的java.sql.Types的SQL类型）
    private Integer dataType;

    // 字段长度
    private Integer columnSize;

    // 字段小数位长度
    private Integer decimalDigits;

    // 字段可否为空 0-NO  1-YES
    private Integer nullAble;

    private String defaultValue;

    // 是否是分区字段
    private Integer isParition = 0;

    // 字段是否是主键
    private boolean isPk;

    // 记录的key，前端使用，无业务意义
    private String key;

    // 字段备注
    private String comment;

    public FieldInfo() {
    }

    public FieldInfo(String fieldName, String tableName, String fieldType) {
        this.fieldName = fieldName;
        this.tableName = tableName;
        this.fieldType = fieldType;
        this.key = this.fieldName;
    }

    public FieldInfo(String fieldName, String tableName, String fieldType, String comment) {
        this.fieldName = fieldName;
        this.tableName = tableName;
        this.fieldType = fieldType;
        this.key = this.fieldName;
        this.comment = comment;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
        this.key = fieldName;
    }

    public void setDataType(Integer dataType) {
        this.dataType = dataType;
        Field[] declaredFields = Types.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            try {
                if (dataType == declaredField.getInt(Types.class)) {
                    this.fieldType = declaredField.getName();
                    break;
                }
            } catch (Exception e) {
            }
        }
    }
}
